#include <bits/stdc++.h>
using namespace std;
const int N=3e5+50;
typedef long long ll;
ll a[N],b[N];
int n,m;
int main(void){
    ll as=0;
    ll bs=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i]);
        as+=a[i];
    }
    scanf("%d",&m);
    for(int i=0;i<m;i++){
        scanf("%lld",&b[i]);
        bs+=b[i];
    }
    if(as!=bs){
        printf("-1\n");
        return 0;
    }
    as=0;
    bs=0;
    int ans=0;
    int i=0,j=0;
    while(i<n && j<m){
        if(as>0 && as==bs){
            ans++;
            as=0;
            bs=0;
        }
        if(as>bs){
            bs+=b[j++];
        }else{
            as+=a[i++];
        }
    }
    if(as>0 || bs>0){
        ans++;
    }
    printf("%d\n",ans);
    return 0;
}